package com.mybatis.annotation;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.mybatis.util.MybatisUtil;

public class Test {
	
/*	@org.junit.Test
	public void testAdd(){
		SqlSessionFactory factory=MybatisUtil.getFactory();
		//默认手动提交的
		SqlSession session=factory.openSession();
		
		String statement = "com.mybatis.demo1.User.userMapper.addUser";
		
		int insert=session.insert(statement,new User(-1, "Kk", 23));
		//提交
		session.commit();
		//关闭session
		session.close();
		System.out.println(insert);
	}
	
	@org.junit.Test
	public void testUpdate(){
		SqlSessionFactory factory=MybatisUtil.getFactory();
		//默认手动提交的,openssion(true)时为自动提交
		SqlSession session=factory.openSession();
		
		String statement = "com.mybatis.demo1.User.userMapper.updateUser";
		
		int update=session.update(statement,new User(3, "Kk44", 28));
		//提交
		session.commit();
		//关闭session
		session.close();
		System.out.println(update);
	}
	
	@org.junit.Test
	public void testDelete(){
		SqlSessionFactory factory=MybatisUtil.getFactory();
		//默认手动提交的,openssion(true)时为自动提交
		SqlSession session=factory.openSession();
		
		String statement = "com.mybatis.demo1.User.userMapper.deleteUser";
		
		int delete=session.delete(statement,3);
		//提交
		session.commit();
		//关闭session
		session.close();
		System.out.println(delete);
	}
	
	@org.junit.Test
	public void testSelect(){
		SqlSessionFactory factory=MybatisUtil.getFactory();
		//默认手动提交的,openssion(true)时为自动提交
		SqlSession session=factory.openSession();
		
		String statement = "com.mybatis.demo1.User.userMapper.getUser";
		
		User user=session.selectOne(statement,2);
		//提交
		session.commit();
		//关闭session
		session.close();
		System.out.println(user);
	}
	
	@org.junit.Test
	public void testGetAll(){
		SqlSessionFactory factory=MybatisUtil.getFactory();
		//默认手动提交的,openssion(true)时为自动提交
		SqlSession session=factory.openSession();
		
		String statement = "com.mybatis.demo1.User.userMapper.getAllUsers";
		
		List<User> list=session.selectList(statement);
		//提交
		session.commit();
		//关闭session
		session.close();
		System.out.println(list);
	}*/
	
	//注解-增加
	@org.junit.Test
	public void testAdd2(){
		SqlSessionFactory factory=MybatisUtil.getFactory();
		//默认手动提交的
		SqlSession session=factory.openSession();
		
		UserMapper mapper=session.getMapper(UserMapper.class);
		int add=mapper.addUser(new User(-1, "张三", 30));
		
		//提交
		session.commit();
		//关闭session
		session.close();
		System.out.println(add);
	}
	
}
